package com.hk.app.utils;

import org.hibernate.annotations.common.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.entity.Organization;
import com.hk.commons.entity.order.OrderInfo;
import com.hk.commons.entity.product.Company;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.OrderStrMsg;
import com.hk.service.IOrganization;
import com.hk.service.product.ICompany;
import com.hk.service.product.IProductInfo;
import com.hk.service.sys.ISysUser;

/**
 * 根据productId 和userId 补全订单相关的属性
 * 
 * @author jeff
 *
 */
public class OrderHelper {
	@Autowired
	private static ISysUser userService;

	@Autowired
	private static IProductInfo productService;

	@Autowired
	private static ICompany comService;

	@Autowired
	private static IOrganization orgService;

	public static OrderStrMsg updOrderStrMsg(OrderStrMsg orderStrMsg) {
		Long userId = null;
		Long productId = null;

		if (StringHelper.isNotEmpty(orderStrMsg.getUserId())) {
			userId = Long.valueOf(orderStrMsg.getUserId());
		}

		if (StringHelper.isNotEmpty(orderStrMsg.getProductId())) {
			productId = Long.valueOf(orderStrMsg.getProductId());
		}
		
		//如果用户或产品不存在，则返回空
		if(userId == null || productId == null ){
			return null;
		}
		
		//01加入顾客 及所属理财师基本属性
		SysUser user = userService.findById(userId);
		if (user != null) {
			orderStrMsg.setUserId(user.getId().toString());
			orderStrMsg.setUserName(user.getRealName());
			orderStrMsg.setUserPhoneNum(user.getName());

			// 理财师信息
			if (user.getRecommendCode() != null) {

				SysUser financialPlanner = userService.getByAccount(user
						.getRecommendCode());
				if (financialPlanner != null) {
					orderStrMsg.setFinancialPlannerId(financialPlanner.getId()
							.toString());
					orderStrMsg.setFinancialPlannerName(financialPlanner
							.getRealName());
					orderStrMsg.setFinancialPlannerPhoneNum(financialPlanner
							.getName());

					if (financialPlanner.getOrgId() != null) {

						Organization org = orgService.findById(financialPlanner
								.getOrgId());
						if (org != null) {
							orderStrMsg.setFinancialPlannerDepName(org
									.getFlag02());
						}

					}
				}
			}
		}

		//02 加入产品信息
		ProductInfo productInfo = productService.findById(productId);
		if (productInfo != null) {
			orderStrMsg.setProductId(productInfo.getId().toString());
			orderStrMsg.setProductName(productInfo.getName());

			orderStrMsg.setProductTerm(productInfo.getProTermStr());
			orderStrMsg.setProductCode(productInfo.getProNo());
			orderStrMsg.setProductTopType(productInfo.getProdTopType()
					.toString());
			Company company = comService.findById(productInfo.getIssuerId());
			if (company != null) {
				orderStrMsg.setProductCompanyId(company.getId().toString());
				orderStrMsg.setProductCompanyName(company.getName());
			}
		}

		return orderStrMsg;
	}

}
